export default {
  namespaced: true,
  state: {
    devices: null,
    domains: null,
    currentResourceId: null
  },
  getters: {
    currentResource (state) {
      let findDevice = (state.devices || []).find(device => {
        return device.id === state.currentResourceId;
      });
      let findDomain = (state.domains || []).find(domain => {
        return domain.id === state.currentResourceId;
      });
      return findDevice || findDomain;
    },
    currentResourcePath (state, getters) {
      let pathArr = [];
      let pathInfo = [];
      let resources = (state.devices || []).concat(state.domains || []);
      let resourceDomain = '';
      if ((getters.currentResource || {}).category === 'Domain') {
        resourceDomain = (getters.currentResource || {}).domainPath || '';
      } else {
        resourceDomain = (getters.currentResource || {}).domains || '';
        resourceDomain += `/${(getters.currentResource || {}).id}`;
      }
      pathArr = resourceDomain.split('/');
      pathArr.filter(path => {
        return path;
      }).forEach(path => {
        let findResource = resources.find(r => {
          return r.id === parseInt(path);
        });
        if (findResource) {
          if (findResource.category === 'Device') {
            pathInfo.push(`${findResource.label}(${findResource.externalDevId})`);
          } else {
            pathInfo.push(findResource.label);
          }
        }
      });
      return pathInfo.join('  >  ');
    },

    /**
     * @author weiloong
     * @description: 根据 modelId===301 获取产线列表
     */
    productionLines (state) {
      let findDomain = (state.domains || []).filter(domain => {
        return domain.modelId === 301;
      });
      return findDomain;
    },

    allDevices (state) {
      return state.devices || [];
    }
  },

  mutations: {
    setDevices (state, devices) {
      state.devices = devices;
    },
    setDomains (state, domains) {
      state.domains = domains;
    },
    setCurrentResourceId (state, id) {
      state.currentResourceId = parseInt(id);
    }
  },
  actions: {}
};
